package com.fingard.dsp.bank.directbank.nnbill02;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.ReqOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.RetAtsOtherElse;

import java.util.HashMap;
import java.util.Map;

public class NNBill02OtherElse extends NNBill02Base implements IOtherElse {

    @Override
    public RetAtsOtherElse d_else(ReqOtherElse p_reqElse) throws Exception {
        RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
        retAts.retHashMap = new HashMap<String, String>();
        try {
            String ownAct = "";
            if (p_reqElse.reqHashMap.containsKey("OurBankAccount")) {
                ownAct = p_reqElse.reqHashMap.get("OurBankAccount");
            }
            String plain = "";
            if (p_reqElse.reqHashMap.containsKey("Plain")) {
                plain = p_reqElse.reqHashMap.get("Plain");
            } else {
                retAts.retHashMap.put("RespCode", "-1");
                retAts.retHashMap.put("RespInfo", "缺失验签原始串");
                return retAts;
            }
            ActSetItem actItem = this.getBankConfig().getActSetItem(ownAct, "order");
            if (actItem == null) {
                retAts.retHashMap.put("RespCode", "-1");
                retAts.retHashMap.put("RespInfo", "未找到账号[" + ownAct + "]商户配置信息");
                return retAts;
            }
            Map<String,String> map = convertToMap(plain);
            StringBuffer str = new StringBuffer();
            str.append(map.get("processFlag")).append(map.get("txnType")).append(map.get("orgTxnType"));
            str.append(map.get("amt")).append(map.get("externalTraceNo")).append(map.get("orgExternalTraceNo")).append(map.get("terminalOperId"));
            str.append(map.get("authCode")).append(map.get("RRN")).append(map.get("txnTime")).append(map.get("shortPAN")).append(map.get("responseCode"));
            str.append(map.get("cardType")).append(map.get("issuerId"));
            boolean isSuccss = veriSign(str.toString().getBytes(),map.get("signature").getBytes(),actItem.ownKeyStorePath);
            if(isSuccss){retAts.retHashMap.put("RespCode", "0");
                retAts.retHashMap.put("RespInfo", "验签通过");
            } else {
                retAts.retHashMap.put("RespCode", "-1");
                retAts.retHashMap.put("RespInfo", "验签失败");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.retHashMap.put("RespCode", "-1");
            retAts.retHashMap.put("RespInfo", ex.getMessage());
            WriteBankLogLn(ex);
        }

        return retAts;
    }
}
